﻿// JavaScript Document
$(document).ready(function(){
	var sBox = $('#Search');  //获取搜索部分
	var sInput = $('#Search_Box');  //获取搜索输入框
	var flagInput = false;  //用于判断焦点是否在输入框内
	var optionIndex = 0;   //当前的搜索选项   
	var searchOption = $("#Search_Option");  //获取搜索选项
	var searchP = $('#Search_Option p');  //获取选项列表
	var scanText ;  //设置一个定时器  
	var secondMenu = $('#Second_Menu') ;  //获取二级导航
	var oneMenu = $('#Top_Nav li a').eq(2) ;  //获取第三个一级导航
	var secondFalg = true ;  //判断鼠标是否移出导航
	var allStyle = $('.Pro_Change input') ;  //获取页面上所有类型
	allStyle.mouseover(function(){
		$(this).stop(true).animate({'opacity':'0.7'},'fast');  
	})
	allStyle.mouseout(function(){
		$(this).stop(true).animate({'opacity':'1'},'fast');  
	})
	oneMenu.mouseover(function(){  //鼠标移入出现二级菜单
		secondFlag = false;
		secondMenu.css('display','block');
	})
	oneMenu.mouseleave(function(){  //二级菜单消失
		secondFlag = true;
		setTimeout(function(){
			if(secondFlag)
				secondMenu.css('display','none');
		},1);
	})
	secondMenu.mouseover(function(){  //悬停在二级菜单上时不回撤菜单
		secondFlag = false;
		oneMenu.css('backgroundColor','#415D70');
	});
	secondMenu.mouseleave(function(){  //离开菜单时回撤菜单
		secondFlag = true;
		oneMenu.css('backgroundColor','');
		secondMenu.css('display','none');
	});
	$("#Search_Box").keydown(function (event) {//上下键获取焦点
            var key = event.keyCode;
			scanText = setInterval(function(){
				if($("#Search_Box").val().length > 9)
					$('#Search_Option p').find('span').html($("#Search_Box").val().substring(0,7)+'...');
				else
					$('#Search_Option p').find('span').html($("#Search_Box").val());
				if ($.trim($("#Search_Box").val() ).length == 0)
				{
					clearInterval(scanText);
					searchOption.fadeOut(200);
					return;
				}
				if(flagInput)
					searchOption.fadeIn(200);
			},100)
            if (key == 38) { /*向上按钮*/  
                optionIndex--;  
                if (optionIndex == -1) optionIndex = 2; //到顶了，   
            } else if (key == 40) {/*向下按钮*/  
                optionIndex++;  
                if (optionIndex == 3) optionIndex = 0; //到底了   
            }  
            var li = searchOption.find("p:eq(" + optionIndex + ")");
			//改变选中搜索项的样式
            li.addClass('Search_Option_Hover').siblings().removeClass('Search_Option_Hover');
			$('#Search_What').val(optionIndex);  //改变隐藏表单的传送值
        }); 
	searchP.mouseover(function(){
		$(this).addClass('Search_Option_Hover');
		optionIndex = $(this).index();
		$('#Search_What').val(optionIndex);  //改变隐藏表单的传送值
	})
	searchP.mouseleave(function(){
		$(this).removeClass('Search_Option_Hover');
	})
	searchP.click(function(){
		$('#Search form').submit();
	})
	sBox.mouseover(function(){  //搜索框的滑出
		sBox.stop(true).animate({width:'310px'},'fast');
		sInput.stop(true).animate({width:'200px'},"fast");
		});
	sBox.mouseleave(backBox);
	function backBox(){  //搜索框的回撤
		//alert(!sInput[0].isFocused)
		if(!flagInput)
		{
			sBox.stop(true).animate({width:'120px'},'normal');
			sInput.stop(true).animate({width:'0px'},"normal");
			sInput.fadeOut(0);
		}
	}
	sInput.focus(function(){  //鼠标焦点对准搜索框
		sInput.css("color",'#000');
		if(sInput.val()=="搜索社团、文章、活动...")
			sInput.val("");
		flagInput = true ;
	});
	sInput.blur(function(){   //搜索框失焦
		sInput.css("color",'#CCC');
		if(sInput.val()=="")
			sInput.val("搜索社团、文章、活动...");
		flagInput = false ;
		backBox();
		searchOption.fadeOut(200);
		clearInterval(scanText);
	}
	);
	
	var userInfo = $('#User_Top');  //获取页面的用户信息部分
	var userBox = $('#User_Info_Box');  //获取页面的用户菜单
	var userFlag ;  //用于判断是否完全移出菜单
	if(userInfo.find('> img').attr('src')!=null){
		userInfo.mouseover(function(){  //鼠标移入出现二级菜单
			if(flagInput)
				return;
			userFlag = false;
			userBox.css('display','block');
			userInfo.css('backgroundColor','#415D70');
		})
		userInfo.mouseleave(function(){  //二级菜单消失
			userFlag = true;
			setTimeout(function(){
				if(userFlag)
					userBox.css('display','none');
			},1);
			userInfo.css('backgroundColor','');
		})
		userBox.mouseover(function(){  //悬停在二级菜单上时不回撤菜单
			userFlag = false;
			userInfo.css('backgroundColor','#415D70');
		});
		userBox.mouseleave(function(){  //离开菜单时回撤菜单
			userFlag = true;
			userInfo.css('backgroundColor','');
			userBox.css('display','none');
		});
	}
	
})
	//获取文章领域选取区域
function articleFieldSelect(type){
	var allField = $('.Article_Field span');  //获取页面上所有文章领域
	allField.unbind('click');  //取消所有事件绑定，避免重复
	allField.click(function(){
		if($(this).css('border')=='2px solid rgb(204, 204, 204)'||$(this).css('border')==''){
			if(type=='one'){
				$(this).css({'border':'2px solid #3399FF','color':'#333'}).siblings().css({'border':'2px solid #CCC','color':'#999'});
				$(this.parentNode).find(':hidden').val($(this).index());
			}
			else{
				$(this).css({'border':'2px solid #3399FF','color':'#333'});
				$(this.parentNode).find(':hidden').val($(this.parentNode).find(':hidden').val()+($(this).index())+'+');
			}
		}
		else{
			$(this).css({'border':'2px solid #CCC','color':'#999'});
			if(type=='one')
				$(this.parentNode).find(':hidden').val('');
			else
				$(this.parentNode).find(':hidden').val($(this.parentNode).find(':hidden').val().replace($(this).index()+'+',''));
		}
	})
}
//Ajax请求函数
var loadingInfo = '<img src="images/loading.gif">' ;
var ajaxFlag = true;  //设置一个标记避免重复发起请求
function ajaxBox(obj,type,uid){
	var ajaxUrl ;
	if(!ajaxFlag)
		return;
	ajaxFlag = false;
	var postWay;  //请求方法
	var boxTitle;  //窗口标题
	if(type=="focus"){   //关注内容
		postWay = '求关注' ;
		boxTitle = '设置关注' ;
		ajaxUrl = '/jsp/Pro?lx=attention&action=add&auid='+uid ;
	}
	else if(type=="unfocus"){   //关注内容
		postWay = '取消关注' ;
		boxTitle = '取消关注' ;
		ajaxUrl = '/jsp/Pro?lx=attention&action=del&atid='+uid ;
	}
	else if(type=="join"){
		postWay = '申请加入社团' ;
		boxTitle = '发送加入申请' ;
	}
	else if(type=="exit"){
		postWay = '退出社团' ;
		boxTitle = '退出社团' ;
	}
	else if(type=="SMS"){
		postWay = '发送私信' ;
		boxTitle = '发送私信' ;
	}
	$(obj).boxy();
	var loadingBox = new Boxy(loadingInfo,{'title':boxTitle,closeable:false,draggable:false});
	$.ajax({
		url:ajaxUrl ,
		type:'post',
		async: false ,
		success:function(){
			loadingBox.setContent('<div class="Ajax_Success"><p>'+boxTitle+"成功！！</div>");
			setTimeout(function(){loadingBox.unload();ajaxFlag = true;},1500)
		},
		error:function(){
			loadingBox.setContent('<div class="Ajax_Success" style="background-image:url(images/ajax_erro.jpg);"><p>'+boxTitle+"出错！！</div>");
			setTimeout(function(){loadingBox.unload();ajaxFlag = true;},1500)
		}
	});
}
//申请修改资料函数
function apply(obj,type){
	var applyTitle;
	var name ;
	var type ;
	if(type=='name'){
		applyTitle = '改名申请'
		name = 'Pro?lx=edit&action=alter' ;
		type = 'uname' ;
	}
	else if(type=='mail'){
		applyTitle = '修改邮箱申请'
		name = 'Pro?lx=edit&action=alter' ;
		type= 'uemail' ;
	}
	else if(type=='school'){
		applyTitle = '修改学校申请'
		name = 'Club?sp=alter&action=alter' ;
		type= 'cname' ;
	}else if(type=='cname'){
		applyTitle = '修改社团名称'
		name = 'Club?sp=alter&action=alter' ;
		type= 'cwhere' ;
	}
	var applyInfo = '<form action='+name+' method="post" class="Apply_Box"><input type="hidden" name="type" value='+type+'><table><tr><td align="right">修改为：</td><td><input type="text" style="width:130px;height:25px;border:1px solid #999;" name="uname"></td></tr><tr><td valign="top" align="right">修改理由：</td><td><textarea name="reason"></textarea></td></tr><tr><td></td><td align="right"><input type="submit" class="Focus_Btn" value="提交申请"></td></tr></table></form>'
		$(obj).boxy();
	var applyBox = new Boxy(applyInfo,{'title':applyTitle,modal:true});
}